package com.cloudz.iptv.fragment.header;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.cloudz.iptv.R;
import com.cloudz.iptv.api.v1.HeaderServiceApi;
import com.cloudz.iptv.fragment.BaseFragment;
import com.cloudz.iptv.utils.InPutJson;
import com.cloudz.iptv.utils.ResponseUtils;
import com.cloudz.iptv.utils.RetrofitUtils;
import com.cloudz.iptv.utils.SimplePreference;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @File Name: com.cloudz.iptv.fragment.header
 * @Author: YJ //作者及
 * @Date: 2018-12-12 16:29//完成日期
 * @Description: // 描述
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public class HeaderFragment extends BaseFragment
{

    @BindView(R.id.header_logo)
    ImageView logo;

    @BindView(R.id.header_wd)
    TextView wd;

    @BindView(R.id.header_wz)
    TextView wz;

    @BindView(R.id.header_rq)
    TextView rq;

    @BindView(R.id.header_week)
    TextView week;

    @BindView(R.id.header_time)
    TextView time;

    Date date;



    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

    SimpleDateFormat sdformat = new SimpleDateFormat("HH:mm a", Locale.CHINA);

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_header;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        // 设置logo
        Glide.with(this).load(R.mipmap.logo).placeholder(R.drawable.placeholder).into(logo);

        timer.schedule(task, 1000, 2000); // 1s后执行task,经过1s再次执行

        date = new Date();

        initData();

        getData();
    }

    public void getData()
    {
        HeaderServiceApi headerServiceApi = RetrofitUtils.getInstance().createApi(SimplePreference.getServerUrl(this.getContext()), HeaderServiceApi.class);
        headerServiceApi.getHeader().enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {

                try
                {
                    InPutJson inOutJson = ResponseUtils.getInOutJson(response);

                    if (inOutJson.result)
                    {
                        JSONObject data = inOutJson.getJSONObject();
                        JSONObject weather = data.getJSONObject("weather");

                        String now = weather.getString("now");
                        wd.setText(now);
                        wz.setText(weather.getString("cityName") + " " + weather.getString("cond_txt"));

                        String d = data.getString("date");

                        rq.setText(d);

//                        String time = data.getString("time");
//
//                        date = df.parse(d + " " + time);

                    }
                } catch (Exception e)
                {
                    Log.d("header----->", "获取头部接口失败");
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {

            }
        });

    }

    public void initData()
    {
        date = new Date();
        rq.setText(df.format(date));
        week.setText(getWeekOfDate(date));

        String LgTime = sdformat.format(date);
        time.setText(LgTime);

    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public String getWeekOfDate(Date date)
    {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            if (msg.what == 1)
            {
                initData();
            }
        }
    };

    Timer timer = new Timer();

    TimerTask task = new TimerTask()
    {
        @Override
        public void run()
        {

            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };
}
